﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WuhanIns.ConSite.SiteMarker.Core.Util
{
    public class ApiLogUtil
    {
        public static void WriteInvokeFunctionLog(string functionName, bool isInvokeStart)
        {
            //string funName = System.Reflection.MethodBase.GetCurrentMethod().Name;
            string dt = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff") + "  invoking function " + functionName + (isInvokeStart ? " start " : " end ") + "\r\n";
            WriteStringLog(dt);
        }

        public static void WriteLog(string str)
        {
            string dt = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff ") + str + "\r\n";
            WriteStringLog(dt);
        }

        private static void WriteStringLog(string sdtString)
        {
            int len = sdtString.Length;
            byte[] inputByts = new byte[len];
            inputByts = Encoding.GetEncoding("utf-8").GetBytes(sdtString);
            System.IO.FileStream fs = new System.IO.FileStream(GetLogPath(), System.IO.FileMode.OpenOrCreate);
            fs.Seek(0, System.IO.SeekOrigin.End);
            fs.Write(inputByts, 0, inputByts.Length);
            fs.Close();//关闭流 
        }

        /// <summary>
        /// 服务日志路径
        /// </summary>
        /// <returns></returns>
        public static string GetLogPath()
        {
            string logPath = string.Empty;// ConstArgs.LogPath;
            if (string.IsNullOrEmpty(logPath))
            {
                logPath = System.AppDomain.CurrentDomain.BaseDirectory;
            }
            if (!Directory.Exists(logPath))
                Directory.CreateDirectory(logPath);
            if (!Directory.Exists(logPath + "\\InsDailyReportLog"))
                Directory.CreateDirectory(logPath + "\\InsDailyReportLog");
            string path = logPath + "\\InsDailyReportLog\\log_" + DateTime.Now.ToString("yyyy-MM") + ".txt";
            return path;
        }
    }
}
